/*
Simple JQuery menu.
HTML structure to use:

Notes:

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
//#e29e85  //#a3c0f8  //#a6c7d9
*/

function initMenus() {
 $('ul.list-links ul').hide();
 $.each($('ul.list-links'), function(){
 var cookie = $.cookie(this.id);
 
 //level2 값
 var subMenu= $.cookie("subMenu");
 //alert(cookie);
 
 if(cookie === null || String(cookie).length < 1) {
 $('#' + this.id + '.expandfirst ul:first').show();
 }
 else {
 $('#' + this.id + ' .' + cookie).next().show();
//쿠키값이 널이 아닐때 쿠키값에 해당하는 id에 색 추가
 document.getElementById(cookie).style.backgroundColor = '#a3c0f8'; 
 
 //안쪽 level2 메뉴값이 있으면
 if(subMenu!=null && subMenu!="")
 document.getElementById(subMenu).style.backgroundColor = '#a3c0f8';
 
 }
 });
 $('ul.list-links li a').click(
 function() {
 var checkElement = $(this).next();
 var parent = this.parentNode.parentNode.id;
 //클릭시 색 추가 이벤트
 if(parent!=""){//자식노드 클릭시 색깔 없어지는것을 막기위해 쓰임
	 for(i=1;i<14;i++){//클래스 늘어날시 숫자 늘려줄것(현재 menu.sjp m13까지 있음)
		if(("m"+i)!=this.className){//클래스 네임값이 같지 않으면 힌색
			if(document.getElementById("m"+i)!=null) // 프로젝트 적용이 안되있어서 클래스가 m1~m13까지 없을때
				document.getElementById("m"+i).style.backgroundColor = '#fff';
		}else{//같으면 지정된 색으로 표시
			document.getElementById("m"+i).style.backgroundColor = '#a3c0f8'; 
		}
	 }
 }
 
 var subMenu= $.cookie("subMenu");
 if(subMenu!=null && subMenu!=""){
	 for(i=1;i<13;i++){//클래스 늘어날시 숫자 늘려줄것(현재 menu.sjp m13까지 있음)
		 //alert("sm"+i);
			if(("sm"+i)!=subMenu){//클래스 네임값이 같지 않으면 힌색
				//alert("다름");
				if(document.getElementById("sm"+i)!=null) // 프로젝트 적용이 안되있어서 클래스가 m1~m13까지 없을때
				document.getElementById("sm"+i).style.backgroundColor='#fcfbf2';
			}else{//같으면 지정된 색으로 표시
				//alert("같음");
				document.getElementById("sm"+i).style.backgroundColor = '#a3c0f8'; 
			}
		 }
 }
 //document.getElementById(this.className).style.backgroundColor = '#a3c0f8'; 
 
 
 
 
 /*
 if($('#' + parent).hasClass('noaccordion')) {
 if((String(parent).length > 0) && (String(this.className).length > 0)) {
 if($(this).next().is(':visible')) {
 $.cookie(parent, null);
 }
 else {
 $.cookie(parent, this.className);
 }
 $(this).next().slideToggle('normal');
 }
 }
 */
 //alert(parent);
 //alert(this.className);


 
 
 if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
 if($('#' + parent).hasClass('collapsible')) {
 $('#' + parent + ' ul:visible').slideUp('normal');
 }
 return false;
 }
 
 
 if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
 $('#' + parent + ' ul:visible').slideUp('normal');
 if((String(parent).length > 0) && (String(this.className).length > 0)) {
 $.cookie(parent, this.className);
 }
 checkElement.slideDown('normal');
 return false;
 }else{
	 //클래스 바껴질때 여기도 바꿀껏(강제로 하나인곳도 쿠키저장하는 부분)
	 //alert(this.className);
	 if(this.className=="m1" || this.className=="m2" || this.className=="m6" || this.className=="m8" || this.className=="m10" || this.className=="m11" || this.className=="m12"  || this.className=="m13"){
		 $('#' + parent + ' ul:visible').slideUp('normal');
		 $.cookie(parent, this.className);
		 $.cookie("subMenu", "");
		 //return false;
	 }else{
		 $.cookie("subMenu", this.className);
	 }
	 
 }
 }
 );
}



//else{
//if(this.className!="m1" && this.className!="m2" && this.className!="m6" && this.className!="m8" && this.className!="m10" && this.className!="m11" && this.className!="m12"  && this.className!="m13"){
//	 alert(this.className);
//	 $.cookie("subMenu", this.className);
//}
//}

/*
function initMenus1() {
 $('ul.list-links1 ul').hide();
 $.each($('ul.list-links1'), function(){
 var cookie = $.cookie(this.id);
 if(cookie === null || String(cookie).length < 1) {
 $('#' + this.id + '.expandfirst1 ul:first').show();
 }
 else {
 $('#' + this.id + ' .' + cookie).next().show();
 }
 });
 $('ul.list-links1 li a').click(
 function() {

 var checkElement = $(this).next();
 var parent = this.parentNode.parentNode.id;
 if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
 if($('#' + parent).hasClass('collapsible')) {
 $('#' + parent + ' ul:visible').slideUp('normal');
 }
 return false;
 }
 if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
 $('#' + parent + ' ul:visible').slideUp('normal');
 if((String(parent).length > 0) && (String(this.className).length > 0)) {
 $.cookie(parent, this.className);
 }
 checkElement.slideDown('normal');
 return false;
 }
 }
 );
}
 */
$(document).ready(function() {initMenus();}); 
//$(document).ready(function() {initMenus1();}); 